package Server.controller;

import Plug.operLog.OperLog;
import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.ShopCarDao;
import Server.dao.ShopSpecDao;
import Server.dao.UsersDao;
import Server.entity.*;
import Server.tool.BeanToJson;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("CarController")
public class CarController {
    @Autowired
    private ShopCarDao shopCarDao;
    @Autowired
    private ShopSpecDao shopSpecDao;
    @Autowired
    private UsersDao usersDao;
    @OperLog("增加商品到购物车")
    @RequestMapping("AddCar")
    public RetResult AddCar(@RequestBody List<ShopSpec> shopSpecList){
        Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        fspuser=usersDao.findByUuid(fspuser.getUuid());
        for(ShopSpec shopSpec:shopSpecList){
            if(shopSpec.getBuyCount()<=0){
                continue;
            }
            ShopCar sp = shopCarDao.findByUsersAndShopSpec(fspuser, shopSpecDao.findByUuid(shopSpec.getUuid()));
            if(sp!=null){
                sp.setCount(sp.getCount()+shopSpec.getBuyCount());
                shopCarDao.save(sp);
            }else{
                ShopCar shopCar = new ShopCar();
                shopCar.setShopSpec(shopSpecDao.findByUuid(shopSpec.getUuid()));
                shopCar.setUsers(fspuser);
                shopCar.setCount(shopSpec.getBuyCount());
                shopCarDao.save(shopCar);
            }
        }
        return RetResponse.makeOKRsp();
    }
    @OperLog("购物车删除")
    @RequestMapping("Del")
    public RetResult Del(@RequestBody ShopCar shopCar){
        shopCarDao.delete(shopCarDao.findByUuid(shopCar.getUuid()));
        return RetResponse.makeOKRsp();
    }
    @OperLog("购物车修改")
    @RequestMapping("Count")
    public RetResult Count(@RequestBody ShopCar shopCar){
        ShopCar entity = shopCarDao.findByUuid(shopCar.getUuid());
        entity.setCount(shopCar.getCount());
        shopCarDao.save(entity);
        return RetResponse.makeOKRsp();
    }
    @RequestMapping("GetCars")
    public RetResult GetCars(){
        Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        fspuser=usersDao.findByUuid(fspuser.getUuid());
        List<ShopCar> shopCars = shopCarDao.findByUsers(fspuser);
        return RetResponse.makeOKRsp(shopCars);
    }
    @RequestMapping("List")
    public RetResult List(@RequestBody JSONObject jsonObject){
        ShopCar entity = (ShopCar) BeanToJson.jsonTobean(jsonObject.getJSONObject("common").toString(), ShopCar.class);
        int page = Integer.parseInt(jsonObject.getString("page")) - 1;
        int size = Integer.parseInt(jsonObject.getString("limit"));
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "uuid");
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("sale.name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("users.name", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<ShopCar> example = Example.of(entity, matcher);
        Page<ShopCar> userlist = shopCarDao.findAll(example, pageable);
        return RetResponse.makeRsp(0, "成功", userlist.getContent(), (int) userlist.getTotalElements());
    }
}
